﻿
Partial Class WebForm_SelfManageWorkHeader_SelfRegionType
    Inherits System.Web.UI.UserControl

    Private Function GetRegionType(ByVal value As String) As System.Data.DataTable

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select b.* ")
        sql.AppendFormat("from CodeMapping a inner join CodeMapping b ")
        sql.AppendFormat("on a.TableName+'_'+a.ColumnName+'_'+a.Value=b.TableName ")
        sql.AppendFormat("where a.TableName='FindDirtyHeader' ")
        sql.AppendFormat("and a.ColumnName='RegionGroup' ")
        sql.AppendFormat("and a.Value='{0}' ", value)
        sql.AppendFormat("and a.Active = 'Y' and b.Active = 'Y' ")

        Return dat.GetData(sql)

    End Function

    Protected Sub rbList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To Me.gvRegionType.Rows.Count - 1

            If i = DirectCast(DirectCast(sender, RadioButtonList).NamingContainer, GridViewRow).RowIndex Then
            Else
                For j As Integer = 0 To TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items.Count - 1
                    TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Selected = False
                Next


            End If
        Next

        'TryCast(Me.gvRegionType.FooterRow.FindControl("rbListOther"), RadioButtonList).Items(0).Selected = False
        TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text = ""

        Me.RegionText = TryCast(sender, RadioButtonList).SelectedItem.Text


    End Sub

    Public Sub BindgvRegionType()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select * from CodeMapping ")
        sql.AppendFormat("where TableName = 'FindDirtyHeader' ")
        sql.AppendFormat("and ColumnName='RegionGroup' ")
        sql.AppendFormat("and Active='Y' ")

        Me.gvRegionType.DataSource = dat.GetData(sql)
        Me.gvRegionType.DataBind()

    End Sub

    Public Sub Enable(ByVal bool As Boolean)
        Me.gvRegionType.Enabled = bool
    End Sub

    Public Property RegionText() As String
        Get
            'If TryCast(Me.gvRegionType.FooterRow.FindControl("rbListOther"), RadioButtonList).Items(0).Selected Then

            If TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text <> "" Then
                Return TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text
            Else
                Return ViewState("RegionText")
            End If

        End Get
        Set(ByVal value As String)
            ViewState("RegionText") = value
        End Set
    End Property


    Public Sub BindRegionItem(ByVal RegionType As String)

        Dim isother As String = "Y"

        For i As Integer = 0 To Me.gvRegionType.Rows.Count - 1

            For j As Integer = 0 To TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items.Count - 1

                If TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Text = RegionType Then
                    TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Selected = True
                    isother = "N"
                    Return
                Else
                    TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Selected = False
                End If
            Next

        Next

        '選擇為 其他
        If isother = "Y" Then
            'TryCast(Me.gvRegionType.FooterRow.FindControl("rbListOther"), RadioButtonList).Items(0).Selected = True
            TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text = RegionType
        End If

    End Sub

    Protected Sub gvRegionType_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRegionType.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim dt As New System.Data.DataTable

            dt = Me.GetRegionType(TryCast(e.Row.FindControl("lblRegionValue"), Label).Text)

            If dt.Rows.Count > 0 Then
                For i As Integer = 0 To dt.Rows.Count - 1
                    Dim item As New ListItem
                    item.Text = dt.Rows(i)("Text").ToString()
                    item.Value = dt.Rows(i)("Value").ToString()
                    TryCast(e.Row.FindControl("rbList"), RadioButtonList).Items.Add(item)
                Next
            End If

        End If
    End Sub

    Protected Sub gvRegionType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvRegionType.DataBound
        If Me.gvRegionType.Rows.Count > 0 Then
            TryCast(Me.gvRegionType.Rows(0).FindControl("rbList"), RadioButtonList).Items(0).Selected = True

            Me.RegionText = TryCast(Me.gvRegionType.Rows(0).FindControl("rbList"), RadioButtonList).SelectedItem.Text
        End If
    End Sub

    Protected Sub rbListOther_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        For i As Integer = 0 To Me.gvRegionType.Rows.Count - 1
            For j As Integer = 0 To TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items.Count - 1
                TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Selected = False
            Next

        Next

    End Sub

End Class


